var sex;
var emId;
window.onload = function () {
    findByItem(1);
}
function changeFace() {
    var reader = new FileReader();//定义文件读取流
    reader.readAsDataURL(document.getElementById("file").files[0]);
    reader.onload = function (ev) {
        document.getElementById("faceImg").src = ev.target.result;
    }
}
async function add() {
    var forObj = new FormData();
    forObj.append("uploadImg",$n("file").files[0]);

    let config = {
        headers: {'Content-Type': 'multipart/form-data'}
    }
    var uploadFileName;

    await axios.post("/upload", forObj, config).then(resp => {
        uploadFileName = resp.data;
    });

    axios.get("/student/add",{
        params:{
            studentName: $n("addName").value,
            studentPhone: $n("addPhone").value,
            sex: sex,
            classId:$n("addClass").value,
            roomId:$n("addRoom").value,
            studentImg: uploadFileName
        }
    }).then(resp=>{
        var info = resp.data;
        if (info=="ok"){
            $n("addDiv").style.visibility="hidden";
            findByItem(1);
        }
    })


}
function findByItem(pageNo) {
    axios.get("/student/findByItem",{
        params: {
            pageNo:pageNo,
            name:$n("nameTxt").value,
            className:$n("classTxt").value,
            room:$n("roomTxt").value
        }
    }).then(resp=>{
        var info = resp.data;
        var str="";
        info.list.forEach(n=>{
            str+= `<tr><td>${n.studentName}</td><td>${n.sex}</td><td>${n.classBean.className}</td><td>${n.roomBean.roomName}</td>
<td>${n.studentPhone}</td><td><input type="button" value="学生退房" style="margin-left:20px;height: 30px" onclick="del(${n.id})">
<input type="button" value="学生换房" style="margin-left:20px;height: 30px" onclick="findById(${n.id})">
<input type="button" value="详细信息" style="margin-left:20px;height: 30px" onclick="findByStId(${n.id})"></td></tr>`
        });
        $n("stuData").innerHTML=str;
        var custr ="";
        for (var i=1;i<=info.totalPage;i++){
            custr+=`<a href="javascript:findByItem(${i})" style="margin-left: 10px;color:black">${i}</a>`;
        }
        $n("stuCutDiv").innerHTML=custr;
    });
}
function del(id) {
    axios.get("/student/del",{
        params: {
            id
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            findByItem(1);
        }
    })
}

function findById(id) {
    emId=id;
    axios.get("/student/findById",{
        params:{
            id
        }
    }).then(resp=>{
        var info = resp.data;
         $n("updateName").innerHTML=info.studentName;
         $n("updateSex").innerHTML=info.sex;
         $n("updateRoom").innerHTML=info.roomBean.roomName;
         $n("updateClass").innerHTML=info.classBean.className;
         $n("updateImg").src="/emFace/"+info.studentImg;
        axios.get("/room/findBySex",{
            params:{
                sex:info.sex
            }
        }).then(resp=>{
            var infos = resp.data;
            var str="";
            infos.forEach(n=>{
                str+=`<option value="${n.id}">${n.roomName}</option>`;
            });
            $n("newRoom").innerHTML=str;
        })
    });
    $n("updateDiv").style.visibility="visible";
}
function update() {
    axios.get("/student/updateRoom",{
        params:{
            id:emId,
            roomId:$n("newRoom").value
        }
    }).then(resp=>{
        var info = resp.data;
        if (info=="ok"){
            findByItem(1);
            updateDivHidden();
        }
    });
}
function findByStId(id) {
    axios.get("/student/findById",{
        params:{
            id
        }
    }).then(resp=>{
        var info = resp.data;
        $n("findName").innerHTML=info.studentName;
        $n("findSex").innerHTML=info.sex;
        $n("findRoom").innerHTML=info.roomBean.roomName;
        $n("findClass").innerHTML=info.classBean.className;
        $n("findImg").src="/emFace/"+info.studentImg;
        $n("findDate").innerHTML=info.studentDate;
        $n("findPhone").innerHTML=info.studentPhone;
    });
    $n("findDiv").style.visibility="visible";

}



function updateDivHidden() {
    $n("updateDiv").style.visibility="hidden";
}
function findDivHidden() {
    $n("findDiv").style.visibility="hidden";
}
function addDiv() {
    $n("addDiv").style.visibility="visible";
    addSexMan();
    classFind();
}
function addStu() {
    $n("addDiv").style.visibility="hidden";
}

function roomSexFind() {
    axios.get("/room/findBySex",{
        params:{
            sex:sex
        }
    }).then(resp=>{
        var infos = resp.data;
        var str="";
        infos.forEach(n=>{
            str+=`<option value="${n.id}">${n.roomName}</option>`;
        });
        $n("addRoom").innerHTML=str;
    });
}
function classFind() {
    axios.get("/class/findAll").then(resp=>{
        var cs = resp.data;
        var str= "";
        cs.forEach(n=>{
            str+=`<option value="${n.id}">${n.className}</option>`;
        })
        $n("addClass").innerHTML=str;
    });
}
function addSexMan() {
    sex="男";
   roomSexFind()
}
function addSexWoman() {
    sex="女";
    roomSexFind()
}